package com.cloud.phone.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.blankj.utilcode.util.LogUtils
import com.cloud.phone.MainActivity
import com.cloud.phone.R
import com.cloud.phone.manager.UserInfoManager
import com.cloud.phone.websocket.WebsocketUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @auth: njb
 * @date: 2024/4/17 14:20
 * @desc: 后台进程保活服务
 */

class CloudService : Service() {
    private val TAG = "CloudService"
    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
    }
    private fun isNewVersion() {
        MainScope().launch {
            withContext(Dispatchers.IO) {
                val isUpdate = UserInfoManager.getAppIsUpdate()
                LogUtils.d(TAG, "是否是新版本$isUpdate")
                if (isUpdate) {
                    startActivityFromService()
                    LogUtils.d(TAG,"===app更新完成启动新版本===")
                }
            }
        }
    }

    private fun startActivityFromService() {
        val intent = Intent(this@CloudService, MainActivity::class.java)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
        UserInfoManager.setAppIsUpdate(false)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (!WebsocketUtil.getWebsocketUtil().isInit) {
            WebsocketUtil.getWebsocketUtil().init(MainActivity.instance)
        }
        isNewVersion()
        val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val channelName = "cloud-phone"
        val channelId = "cloud-10"
        val notifyId = 100
        val text = "正在运行"
        when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { //适配8.0及以上
                manager.createNotificationChannel(
                    NotificationChannel(
                        channelId, channelName,
                        NotificationManager.IMPORTANCE_DEFAULT
                    )
                )
                startForeground(
                    notifyId, Notification.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        //.setContentText(text)
                        .setSubText(text)
                        .build()
                )
            }

            else -> {
                val notification = NotificationCompat.Builder(this, channelId)
                    .setContentTitle(channelName)
                    .setSubText(text)
                    //.setContentText(text)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build()
                manager.notify(notifyId, notification)
            }
        }
        return START_STICKY
    }
}